﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 僵尸的状态
/// </summary>
public enum ZombleStyle
{
    IDEL,
    WALK,
    ATTACK,
    DEAD
}
public class Zomble : MonoBehaviour
{
    Animator animator;
    private bool caneat;
    public Grid currGrid; 
    private ZombleStyle style; 
    private float attck = 100; 
    private float speed=-6;
    private int attackvalue = 100  ;
    bool isAttack = false;
    private string moveStyle;

    public ZombleStyle Style {
        get => style;
        set {
            style = value;
            switch(style)
            {
                case ZombleStyle.IDEL:
                    animator.Play(moveStyle,0,0);
                    animator.speed = 0;
                    break;
                case ZombleStyle.ATTACK:
                    animator.Play("攻击");
                    animator.speed = 1;
                    break;
                case ZombleStyle.DEAD:
                    animator.Play("");
                    break;
                case ZombleStyle.WALK:
                    animator.Play(moveStyle);
                    animator.speed = 1; 
                    break;
            }
        }
    }

    private void Start()
    {
        ZmobleManager.instance.AddZomble(this);
        currGrid = new Grid(Vector2.zero, Vector2.zero,false);
        animator = GetComponentInChildren<Animator>();
        GetGridByVertical(0);
        int randomWalk = Random.Range(1, 4);//随机三种走路方式
        switch(randomWalk)
        {
            case 1:
                moveStyle = "僵尸";
                break;
            case 2:
                moveStyle = "普通僵尸移动 1";
                break;
            case 3:
                moveStyle = "普通僵尸移动 2";  
                break;
        }
        animator.Play(moveStyle);

    }
    /// <summary>
    /// 状态监测
    /// </summary>
    private void FSM()
    {
        switch(style)
        {
            case ZombleStyle.IDEL:
                style = ZombleStyle.WALK;
                break;
            case ZombleStyle.ATTACK:
                if (isAttack) break;
                Attack(currGrid.CurrPlantBase);  
                break;
            case ZombleStyle.DEAD:
                animator.Play("");
                break;
            case ZombleStyle.WALK:
                Move(); 
                break;
        }
    }
    private void Update()
    {
        FSM();
    }
    void Move()
    {
        animator.Play(moveStyle); 
        if (currGrid == null) { return; } 
        else
        {
        currGrid = GridManager.instance.GetGridByWordPos(transform.position); 
        }

        //攻击植物(网格有植物 植物在左边 距离小于0.3)
        if (caneat)
        {
            style = ZombleStyle.ATTACK;
            return;
        }
        transform.Translate(new Vector3(1.5f, 0) * (Time.deltaTime / 1) / speed);

    }
    void Attack(Base_ plant)
    {
        isAttack = true;
        animator.Play("攻击");
        StartCoroutine(Dohurt(plant));
    }
    /// <summary>
    /// 获取出生的位置
    /// </summary>
    /// <param name="num"></param>
    private void GetGridByVertical(int num)
    {
        currGrid= GridManager.instance.GridByVertical(num);
        transform.position = new Vector3(transform.position.x, currGrid.Point.y);
    }
    /// <summary>
    /// 攻击
    /// </summary>
    /// <returns></returns>
    IEnumerator Dohurt(Base_ plant)
    {

        while (plant.hp > 0)
        {
            plant.Hurt(attackvalue / 5);
            yield return new WaitForSeconds(0.2f);
        }

        isAttack = false;
        caneat = false;
        style = ZombleStyle.WALK;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "植物")
        {
            caneat = true;
        }
    }
}
 